package cn.schoolwow.util.test.module.query.jsonarray.flow.query;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.jsonarray.QueryJSONArray;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Assert;

import java.util.Objects;

public class JSONQueryListFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        JSONArray userArray = (JSONArray) flowContext.checkData("userArray");
        String password = (String) flowContext.checkData("password");

        QueryJSONArray queryJSONArray = QueryJSONArray.newQuery(userArray)
                .addQuery("password", password);
        long count = queryJSONArray.execute().count();
        Assert.assertEquals(userArray.size(), count);

        JSONArray queryUserArray = queryJSONArray
                .underline()
                .execute()
                .getArray();
        Assert.assertEquals(userArray, queryUserArray);
        for(int i=0;i<queryUserArray.size();i++){
            JSONObject o = queryUserArray.getJSONObject(i);
            Assert.assertTrue(o.containsKey("user_id"));
            Assert.assertNotNull(o.getLong("user_id"));
        }
    }

    @Override
    public String name() {
        return "用户JSON列表查询";
    }
}
